import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
// import { User } from '@prisma/client'; // 已无 User model，移除
import config from '../config';

// 密码加密
export const hashPassword = async (password: string): Promise<string> => {
  const saltRounds = 10;
  return bcrypt.hash(password, saltRounds);
};

// 验证密码
export const verifyPassword = async (plainPassword: string, hashedPassword: string): Promise<boolean> => {
  return bcrypt.compare(plainPassword, hashedPassword);
};

// // 生成JWT令牌
// export const generateToken = (user: User): string => {
//   const secret = config.jwt.secret || 'your_jwt_secret';
//   return jwt.sign(
//     { id: user.id, username: user.username, email: user.email },
//     secret,
//     { expiresIn: '24h' }
//   );
// };

// 验证JWT令牌
export const verifyToken = (token: string): any => {
  const secret =config.jwt.secret || 'your_jwt_secret';
  try {
    return jwt.verify(token, secret);
  } catch (error) {
    console.error('Token verification failed:', error);
    return null;
  }
}; 